using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Management;
using System.Resources;
using System.Windows.Forms;
using Infragistics.Win;
using Infragistics.Win.UltraWinScrollBar;
using Infragistics.Win.UltraWinTree;

namespace EditorButtonsDemo
{
	public class UltraExplorerTree : UltraTree
	{
		private const string FolderAppearanceKey = "SystemFolder";

		private const string CdAppearanceKey = "SystemCD";

		private const string FloppyAppearanceKey = "SystemRemovable";

		private const string LocalDiskAppearanceKey = "SystemLocalDisk";

		private const string NetworkAppearanceKey = "SystemNetworkDisk";

		private const string ExpandedFolderAppearanceKey = "SystemFolderExpanded";

		private const string RemovableHardDiskAppearanceKey = "SystemRemovableDisk";

		private ImageList ilDirectory;

		private IContainer components;

		private bool isInitialized;

		private void InitializeComponent()
		{
			this.components = new System.ComponentModel.Container();
			System.Resources.ResourceManager resourceManager = new System.Resources.ResourceManager(typeof(EditorButtonsDemo.UltraExplorerTree));
			this.ilDirectory = new System.Windows.Forms.ImageList(this.components);
			((System.ComponentModel.ISupportInitialize)this).BeginInit();
			this.ilDirectory.ColorDepth = System.Windows.Forms.ColorDepth.Depth8Bit;
			this.ilDirectory.ImageSize = new System.Drawing.Size(16, 16);
			this.ilDirectory.ImageStream = (System.Windows.Forms.ImageListStreamer)resourceManager.GetObject("ilDirectory.ImageStream");
			this.ilDirectory.TransparentColor = System.Drawing.Color.Lime;
			((System.ComponentModel.ISupportInitialize)this).EndInit();
		}

		public UltraExplorerTree()
		{
			InitializeComponent();
		}

		[EditorBrowsable(EditorBrowsableState.Advanced)]
		public new bool ShouldSerializeNodes()
		{
			return false;
		}

		private void Initialize()
		{
			if (!isInitialized)
			{
				isInitialized = true;
				base.Override.SelectionType = SelectType.Single;
				base.Override.AllowCopy = DefaultableBoolean.False;
				base.Override.AllowCut = DefaultableBoolean.False;
				base.Override.AllowDelete = DefaultableBoolean.False;
				base.Override.AllowPaste = DefaultableBoolean.False;
				base.Override.ShowExpansionIndicator = ShowExpansionIndicator.CheckOnExpand;
				base.ImageList = ilDirectory;
				CreateAppearances();
				CreateRootNodes();
			}
		}

		private void CreateRootNodes()
		{
			base.Nodes.Clear();
			try
			{
				base.Nodes.AddRange(CreateRootNodesViaManagement());
			}
			catch
			{
			}
		}

		private void CreateAppearances()
		{
			CreateAppearance(0, "SystemRemovable");
			CreateAppearance(1, "SystemLocalDisk");
			CreateAppearance(2, "SystemFolder");
			CreateAppearance(3, "SystemCD");
			CreateAppearance(4, "SystemNetworkDisk");
			CreateAppearance(5, "SystemFolderExpanded");
			CreateAppearance(6, "SystemRemovableDisk");
		}

		private void CreateAppearance(int imageIndex, string key)
		{
			int num = base.Appearances.IndexOf(key);
			if (num < 0)
			{
				Infragistics.Win.Appearance appearance = new Infragistics.Win.Appearance(key);
				appearance.Image = imageIndex;
				base.Appearances.Add(appearance);
			}
		}

		private UltraTreeNode[] CreateRootNodesViaManagement()
		{
			ManagementObjectSearcher managementObjectSearcher = null;
			ManagementObjectCollection managementObjectCollection = null;
			ArrayList arrayList = new ArrayList(13);
			try
			{
				managementObjectSearcher = new ManagementObjectSearcher(new ManagementScope(ManagementPath.DefaultPath), new SelectQuery("SELECT Name,DriveType,MediaType FROM Win32_LogicalDisk"));
				managementObjectCollection = managementObjectSearcher.Get();
				foreach (ManagementObject item in managementObjectCollection)
				{
					string key = (string)item["Name"];
					DriveType driveType = (DriveType)Enum.ToObject(typeof(DriveType), item["DriveType"]);
					UltraTreeNode ultraTreeNode = new UltraTreeNode(key, key);
					switch (driveType)
					{
					case DriveType.CompactDisk:
						ultraTreeNode.Override.NodeAppearance = base.Appearances["SystemCD"];
						break;
					case DriveType.LocalDisk:
						ultraTreeNode.Override.NodeAppearance = base.Appearances["SystemLocalDisk"];
						break;
					case DriveType.NetworkDisk:
						ultraTreeNode.Override.NodeAppearance = base.Appearances["SystemNetworkDisk"];
						break;
					case DriveType.RemoveableDisk:
						if (item["MediaType"] == null)
						{
							ultraTreeNode.Override.NodeAppearance = base.Appearances["SystemRemovableDisk"];
							break;
						}
						switch ((uint)item["MediaType"])
						{
						case 12u:
							ultraTreeNode.Override.NodeAppearance = base.Appearances["SystemRemovableDisk"];
							break;
						case 2u:
						case 3u:
						case 4u:
						case 5u:
							ultraTreeNode.Override.NodeAppearance = base.Appearances["SystemRemovable"];
							break;
						}
						break;
					}
					arrayList.Add(ultraTreeNode);
				}
			}
			finally
			{
				managementObjectCollection.Dispose();
				managementObjectSearcher.Dispose();
			}
			arrayList.TrimToSize();
			return (UltraTreeNode[])arrayList.ToArray(typeof(UltraTreeNode));
		}

		private void CreateFolderNodes(UltraTreeNode node)
		{
			string text = node.FullPath;
			AppearanceBase nodeAppearance = null;
			AppearanceBase activeNodeAppearance = null;
			if (node.Control != null)
			{
				nodeAppearance = node.Control.Appearances["SystemFolder"];
				activeNodeAppearance = node.Control.Appearances["SystemFolderExpanded"];
			}
			try
			{
				if (!text.EndsWith("\\"))
				{
					text += "\\";
				}
				string[] directories = Directory.GetDirectories(text);
				node.Override.Sort = SortType.Ascending;
				string[] array = directories;
				foreach (string text2 in array)
				{
					UltraTreeNode ultraTreeNode = new UltraTreeNode(text2, Path.GetFileName(text2));
					if (node.Control != null)
					{
						ultraTreeNode.Override.NodeAppearance = nodeAppearance;
						ultraTreeNode.Override.ActiveNodeAppearance = activeNodeAppearance;
					}
					node.Nodes.Add(ultraTreeNode);
				}
			}
			catch (IOException)
			{
			}
		}

		private void SelectNearestNode(Point pt)
		{
			if (base.SelectedNodes.Count > 0)
			{
				UIElement uIElement = base.SelectedNodes[0].UIElement;
				if (uIElement != null && pt.Y >= uIElement.Rect.Top && pt.Y <= uIElement.Rect.Bottom)
				{
					base.ActiveNode = base.SelectedNodes[0];
					return;
				}
			}
			UIElement uIElement2 = null;
			for (UltraTreeNode nextVisibleNode = base.TopNode; nextVisibleNode != null; nextVisibleNode = nextVisibleNode.NextVisibleNode)
			{
				uIElement2 = nextVisibleNode.UIElement;
				if (uIElement2 == null)
				{
					break;
				}
				if (pt.Y >= uIElement2.Rect.Top && pt.Y <= uIElement2.Rect.Bottom)
				{
					base.SelectedNodes.Clear();
					nextVisibleNode.Selected = true;
					base.ActiveNode = nextVisibleNode;
					break;
				}
			}
		}

		private bool IsOverScrollBar()
		{
			UIElement lastElementEntered = ControlUIElement.LastElementEntered;
			if (lastElementEntered == null)
			{
				return false;
			}
			if (lastElementEntered is ScrollBarUIElement)
			{
				return true;
			}
			return lastElementEntered.GetAncestor(typeof(ScrollBarUIElement)) != null;
		}

		public void SelectFolder(string path)
		{
			UltraTreeNode ultraTreeNode = FindClosestNode(path);
			if (ultraTreeNode != null)
			{
				base.SelectedNodes.Clear();
				ultraTreeNode.Selected = true;
				base.ActiveNode = ultraTreeNode;
				ultraTreeNode.BringIntoView(false);
			}
		}

		private UltraTreeNode FindClosestNode(string path)
		{
			if (!isInitialized)
			{
				Initialize();
			}
			if (base.Nodes.Count == 0)
			{
				return null;
			}
			if (path == null || path.Length == 0)
			{
				if (base.Nodes[0].Visible)
				{
					return base.Nodes[0];
				}
				return base.Nodes[0].NextVisibleNode;
			}
			string[] array = path.Split('\\');
			UltraTreeNode ultraTreeNode = FindChild(base.Nodes, array[0]);
			if (ultraTreeNode == null)
			{
				return null;
			}
			for (int i = 1; i < array.Length; i++)
			{
				UltraTreeNode ultraTreeNode2 = FindChild(ultraTreeNode, array[i]);
				if (ultraTreeNode2 == null)
				{
					return ultraTreeNode;
				}
				ultraTreeNode = ultraTreeNode2;
			}
			return ultraTreeNode;
		}

		private UltraTreeNode FindChild(UltraTreeNode node, string directory)
		{
			if (node == null)
			{
				return null;
			}
			bool flag = false;
			if (node.HasExpansionIndicator && !node.HasNodes)
			{
				node.Expanded = true;
				flag = true;
			}
			UltraTreeNode ultraTreeNode = FindChild(node.Nodes, directory);
			if (ultraTreeNode == null && flag)
			{
				node.Expanded = false;
			}
			return ultraTreeNode;
		}

		private UltraTreeNode FindChild(TreeNodesCollection nodes, string directory)
		{
			if (nodes == null || nodes.Count == 0)
			{
				return null;
			}
			foreach (UltraTreeNode node in nodes)
			{
				if (node.Text == directory)
				{
					return node;
				}
			}
			return null;
		}

		protected override void OnBeforeExpand(CancelableNodeEventArgs e)
		{
			if (e.TreeNode.HasExpansionIndicator && e.TreeNode.Nodes.Count == 0)
			{
				CreateFolderNodes(e.TreeNode);
			}
			base.OnBeforeExpand(e);
		}

		protected override void OnEndInit()
		{
			Initialize();
			base.OnEndInit();
		}

		protected override void OnCreateControl()
		{
			Initialize();
			base.OnCreateControl();
		}

		protected override void OnMouseMove(MouseEventArgs e)
		{
			base.OnMouseMove(e);
			if (!IsOverScrollBar())
			{
				SelectNearestNode(new Point(e.X, e.Y));
			}
		}
	}
}
